<template>
	<view>
		<u-navbar title="注册"></u-navbar>
		<view class="" style="padding: 0 30rpx;">
			<u-form :model="data" ref="uForm">
				<u-form-item v-for="(item,index) in inputs" :key="index" :prop="item.value">
					<view class="u-flex" style="height: 100rpx;flex:1">
						<u-input v-model="data[item.value]" :placeholder="item.placeholder" :type="item.type">
						</u-input>
					</view>
				</u-form-item>
			</u-form>
			<view style="height: 60rpx;">

			</view>
			<u-button type="primary" :loading="loading" @click="Uadd">{{$z.$t("注册")}}</u-button>

		</view>
	</view>
</template>

<script>
	import request from "@/z/function/request";
	export default {
		data() {
			return {
				loading: false,
				data: {
					uUser: "",
					dma: "",
					dmaqr: "",
					uxm: "",
					utel: "",
					bz: '' //邮箱

				},
				inputs: [{
						value: "uxm",
						placeholder: this.$z.$t("姓名")
					}, {
						value: "utel",
						placeholder: this.$z.$t("联系电话")
					},
					{
						value: "bz",
						placeholder: this.$z.$t("邮箱")
					},
					{
						value: "uUser",
						placeholder: this.$z.$t("用户名")
					}, {
						value: "dma",
						type: "password",
						placeholder: this.$z.$t("密码")
					},
					{
						value: "dmaqr",
						type: "password",
						placeholder: this.$z.$t("确认密码")
					}
				],
				rules: {
					uxm: [{
						required: true,
						message: this.$z.$t("请输入姓名"),
						trigger: ['blur', 'change']
					}],
					utel: [{
						required: true,
						message: this.$z.$t("请输入电话"),
						trigger: ['blur', 'change']
					}],
					uUser: [{
						required: true,
						message: this.$z.$t("请输入用户名"),
						trigger: ['blur', 'change']
					}],
					bz: [{
						required: true,
						message: this.$z.$t("请输入邮箱"),
						trigger: ['blur', 'change']
					}],
					dma: [{
							required: true,
							message: this.$z.$t("请输入密码"),
							trigger: ['blur', 'change']
						},
						{
							message: this.$z.$t("密码最少为6位，由数字和字母构成,不能有特殊字符"),
							trigger: ['blur', 'change'],
							validator(rule, value, callback) {
								if (!uni.$u.test.dma(value)) {
									callback(new Error('error'))
								} else {
									callback()
								}
							},
						},
					],
					dmaqr: [{
						message: this.$z.$t("请再次输入密码"),
						trigger: ['blur', 'change'],
						validator: (rule, value, callback) => {
							if (this.data.dma != value) {
								callback(new Error('error'))
							} else {
								callback()
							}
						},
					}, ]
				}
			}
		},
		asyncComputed: {
			// async btndisabled() {
			// for (let i in this.data) {
			// 	if (this.data[i] == "" && !(i == "uxm" || i == "utel")) {
			// 		return true
			// 	}
			// }
			// let valid = await this.$refs.uForm.validate()
			// return !valid
			// }
		},
		mounted() {

		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			async Uadd() {
				await this.$refs.uForm.validate()
				this.loading = true;
				try {
					const [res] = await request("/UaddApp", this.data)
					if (res.par === 'succeed') {
						uni.showToast({
							title: this.$z.$t("注册成功"),
							icon: 'success'
						})
						setTimeout(() => {
							uni.navigateTo({
								url: "/pages/login/login"
							})
						}, 1000)

					} else {
						uni.showToast({
							title: this.$z.$t("注册失败"),
							icon: 'error'
						})
					}
					// await this.$z.requestbase("/UaddApp", this.data)
					// await this.$store.dispatch("user/applogin", this.data)
					this.loading = false;
				} catch (e) {
					//TODO handle the exception
					this.loading = false;
				}

			},
		}
	}
</script>

<style lang="scss" scoped>
	.register {
		height: 60rpx;
		color: #2093f5;
	}
</style>